<?php
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'WSL/Config/Config.php';

class WSL_Config_Test extends PHPUnit_Framework_TestCase
{
    protected $_config;
    protected $_config2;
    protected $_array;
    protected $_array2;
    
    public function setUp()
    {   
        $this->_array = array('section' => array('property1' => 'alice',
                                                 'property2' => 'caterpillar',
                                                 'property3' => 'dormohouse'),
                              'section2' => array('property1' => 'cheshirecat',
                                                  'property2' => 'madhatter'),
                              'section3' => array('property1' => 'whiterabbit',
                                                  'property2' => 'eaglet')
    	                     );
    	              
        $this->_array2 = array('section' => array('property1' => 'lewiscarrol',
                                           'property2' => 'caterpillar'),
                               'section4' => array('property1' => 'dodo',
                                                   'property2' => 'queenofhearts'),
                               'section3' => array('property1' => 'wonderland',
                                                   'property2' => 'eaglet',
                                                   'property3' => 'mockturtle')
    	                      );
    	                     
    	$file1 = $this->createFile('file1.ini', $this->_array);
    	$file2 = $this->createFile('file2.ini', $this->_array2);
    	
    	$this->_config  = new WSL_Config($file1);
        $this->_config2 = new WSL_Config($file2);
    }
    
    public function testConstruct()
    {
    	$fileName     = rand();
        $msgException = 'The file does not exist: '. $fileName;
        $this->setExpectedException('Exception', $msgException);

    	$config = new WSL_Config($fileName);    	
    }
    
    public function testtoArray()
    {
        $this->assertEquals($this->_config->toArray(), $this->_array);
        $this->assertEquals($this->_config2->toArray(), $this->_array2);
    }
    
    public function testgetSection()
    {
        $this->assertEquals($this->_config->getSection('section'),
                            $this->_array['section']);
                            
        $this->assertEquals($this->_config->getSection(rand()),
                            array());
    }
    
    public function testgetValue()
    {
        $this->assertEquals($this->_config->getValue('section', 'property1'),
                            'alice');
    }
    
    public function testsetValue()
    {
    	$this->_config->setValue('section', 'property1', 'foo');
    	$this->assertEquals($this->_config->getValue('section', 'property1'), 'foo');
    }
    
    public function testgetFileName()
    {
    	$this->assertEquals($this->_config->getFileName(), 'file1.ini');
    	$this->assertEquals($this->_config2->getFileName(), 'file2.ini');
    }
    
    public function testmerge()
    {
    	$config  = $this->_config;
    	$config2 = $this->_config2;
    	
    	$config->merge($config2);

    	$this->assertEquals($this->_config->getValue('section', 'property1'), 'lewiscarrol');
    	$this->assertEquals($this->_config->getValue('section2', 'property2'), 'madhatter');
    	$this->assertEquals($this->_config->getValue('section3', 'property3'), 'mockturtle');
    }
    
    public function testmergeWithNoOverride()
    {
    	$config  = $this->_config;
    	$config2 = $this->_config2;
    	
    	$config->merge($config2, false);

    	$this->assertEquals($this->_config->getValue('section', 'property1'), 'alice');
    	$this->assertEquals($this->_config->getValue('section2', 'property2'), 'madhatter');
    	$this->assertEquals($this->_config->getValue('section3', 'property3'), 'mockturtle');
    }
    
    public function createFile($fileName, $array)
    {
        touch($fileName);
    	$file = fopen($fileName, 'w+');
    	
    	foreach ($array as $key => $items) {
    		fwrite($file, '['.$key.']'."\n\r");
    		foreach ($items as $key => $value) {
    			fwrite($file, $key. '=' .$value ."\n\r");
    		}
    	}
    	
    	return $fileName;
    }
    
    public function removeFile($fileName)
    {
    	unlink($fileName);
    }
    
    public function tearDown()
    {
    	$this->removeFile('file1.ini');
    	$this->removeFile('file2.ini');
    	
        unset($this->_config);
        unset($this->_config2);
    }

}